package com.xingkeduo.business.transformation.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.dto.BaseDTO;
import com.xingkeduo.pojo.query.CraftsmanQO;
import com.xingkeduo.pojo.query.CraftsmanServiceQO;
import com.xingkeduo.pojo.table.CraftsmanServiceTO;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 发型师服务
 *
 * @author 高杰
 * @time 2018年12月25日 下午1:45:17
 * @description
 */
@Component
public class CraftsmanServiceJTO extends BaseJson {

    public static CraftsmanServiceTO createTableForNew(String json) throws Throwable {
        CraftsmanServiceTO table = new CraftsmanServiceTO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        table.setCraftsmanId(validPrimary(root, "craftsmanId", YesNoEnum.NO))
                .setServiceId(validPrimary(root, "serviceId", YesNoEnum.NO)).setStuatus(validStatus(root, YesNoEnum.NO))
                .setIsDelete(YesNoEnum.NO).setShopId(validPrimary(root, "serviceId", YesNoEnum.NO));

        return table;
    }

    public static CraftsmanServiceQO createQueryForCraftsmanList(String json) throws Throwable {
        CraftsmanServiceQO table = new CraftsmanServiceQO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        table.setServiceId(validPrimary(root, "serviceId", YesNoEnum.NO)).setShopId(validPrimary(root,"shopId",YesNoEnum.NO));
        return table;
    }
    
    public static CraftsmanServiceQO createQueryList(String json) throws Throwable {
        CraftsmanServiceQO table = new CraftsmanServiceQO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        table.setCraftsmanId(validPrimary(root, "craftsmanId", YesNoEnum.NO))
                .setServiceId(validPrimary(root, "serviceId", YesNoEnum.NO))
                .setIsDelete(YesNoEnum.NO).setShopId(validPrimary(root, "shopId", YesNoEnum.NO))
                .setStatus(validStatus(root, YesNoEnum.NO));
        return table;
    }

    public static CraftsmanServiceQO createQueryForList(String json) throws Throwable {
        CraftsmanServiceQO query = new CraftsmanServiceQO();
        JsonNode root = validJson(json);
        if (root == null)
            return query;

        query.setCraftsmanId(validPrimary(root, "craftsmanId", YesNoEnum.NO)).setShopId(validPrimary(root, "shopId", YesNoEnum.NO));
        if (root.has("serviceIds")) {
            if (!root.path("serviceIds").isArray()) {
                throw new InputParamException(InputParamException.Type.INVALID, "服务技能集合");
            } else {
                List<Long> list = new ArrayList<>(10);
                JsonNode ids = root.path("serviceIds");
                ids.forEach((JsonNode node) -> {
                    list.add(Long.parseLong(String.valueOf(node)));
                });
                query.setServiceIds(list);
            }
        }
//        else{
//            throw new InputParamException(InputParamException.Type.INVALID, "服务技能数据错误");
//        }
        if (root.has("serviceList")) {
            if (!root.path("serviceList").isArray()) {
                throw new InputParamException(InputParamException.Type.NULL, "调价");
            } else {
                List<BaseDTO> list = new ArrayList<>(10);
                JsonNode ids = root.path("serviceList");
                ids.forEach((JsonNode node) -> {
                    BaseDTO baseDTO = new BaseDTO();
                    baseDTO.setId(String.valueOf(node.get("id")));
                    baseDTO.setName(String.valueOf(node.get("name")));
                    baseDTO.setValue(String.valueOf(node.get("prices")));
                    list.add(baseDTO);
                });
                query.setServiceList(list);
            }
        }
        return query;
    }


    public static CraftsmanQO createQueryForPrimary(String json) throws Throwable {
        CraftsmanQO query = new CraftsmanQO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        query.setId(validId(root, YesNoEnum.YES));

        return query;
    }

    public static ObjectNode toJson(CraftsmanServiceTO table) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();

        if (table != null) {
            root.put("craftsmanId", table.getCraftsmanId());
            root.put("shopId", table.getShopId());
            if (null != table.getPrices()) {
                root.put("price", table.getPrices() == null ? new BigDecimal(1) : table.getPrices());
            }
            root.put("serviceId", table.getServiceId());
            root.put("status", table.getStatus().getValue());
        }
        toBaseJson(root, table);
        return root;
    }

    public static ArrayNode toJson(List<CraftsmanServiceTO> tables) throws Throwable {
        ArrayNode root = MAPPER.createArrayNode();

        if (!CommonUtil.isEmpty(tables))
            for (Iterator<CraftsmanServiceTO> iterator = tables.iterator(); iterator.hasNext(); ) {
                root.add(toJson(iterator.next()));
            }

        return root;
    }

    /**
     * @param node:
     * @param isRequired:
     * @return : java.util.Set<com.xingkeduo.pojo.enums.ServiceEnums>
     * @author : gaozifeng
     * @date : 2019/5/6 21:33
     */
    public static YesNoEnum validStatus(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "status";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!YesNoEnum.validValue(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return YesNoEnum.getYesNo(value);
        }
        return null;
    }

    /**
     * 传list
     *
     * @param node:
     * @param isRequired:
     * @return : java.util.List<com.xingkeduo.pojo.dto.BaseDTO>
     * @author : gaozifeng
     * @date : 2019/5/6 21:33
     */
//    public static List<BaseDTO> validList(JsonNode node, YesNoEnum isRequired) {
//        String key = "serviceList";
//        if (YesNoEnum.YES == isRequired && !node.has(key))
//            throw new InputParamException(InputParamException.Type.NULL, key);
//
//        if (node.has("serviceList")) {
//            if (!node.path("serviceList").isArray()) {
//                throw new InputParamException(InputParamException.Type.NULL, "调价");
//            } else {
//                List<Long> list = new ArrayList<>(10);
//                JsonNode ids = node.path("type");
//                JsonNode name = node.path("name");
//            }
//        }
//    }
}
